<?php

/**
 * Project: Espolon
 * Licence: BSD License
 * Link: http://espolon.org/
 * Copyright: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Author: Nicolas de Bari Embriz Garcia Rojas <nbari at espolon dot org>
 * Version: Beta
 */

if (!defined('SITE_ROOT')) exit('No direct script access allowed');

class Modules extends Dispatcher {
  private $id;
  private $name;
  private $msg;

  function setId ($id) { $this->id = $id; }
  function setName ($name) { $this->name = $name; }
  function setMsg ($msg) {$this->msg = $msg; }

  function getId() { return $this->id; }
  function getName() { return $this->name; }
  function getMsg() { return $this->msg; }

  function getModules($gid=null) {
    if (isset($gid)) {
      return $this->getDb()->GetCol("SELECT mid FROM core_module_groups_modules WHERE gid=$gid");
    } else {
      $rs = $this->getDb()->GetAll("SELECT * FROM core_modules WHERE name NOT IN('admin','user','main') ORDER BY name");
      if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); } else { return $rs; }
    }
  }

  function getRoleName() {
    $rs = $this->getDb()->GetOne("SELECT name FROM core_roles WHERE rid='".$this->getRid()."'");
    if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); } else { return $rs; }
  }

  function checkExists($op) {
    if ($op == 'name') {
      return $this->getDb()->GetRow("SELECT * FROM core_module_groups WHERE name='".$this->getName()."'") ? false : true;
    } else {
      return $this->getDb()->GetRow("SELECT * FROM core_module_groups WHERE name='".$this->getName()."' AND id !='".$this->getId()."'") ? false : true;
    }
  }

  function addGroup($values) {
    $data['name'] = $values['name'];
    $data['description'] = $values['description'];
    $data['rank'] = $this->getDb()->GetOne("SELECT MAX(rank) FROM core_module_groups") + 1;
    $rs = $this->getDb()->AutoExecute('core_module_groups', $data, 'INSERT');
    $this->setId($this->getDb()->Insert_Id());
    $id = $this->getId();
    $i = 1;
    foreach($values['checkbox'] as $key => $value) {
      $rs = $this->getDb()->Execute("INSERT INTO core_module_groups_modules SET gid='$id', mid='$key', rank=$i");
      $i++;
    }
  }

  function modifyGroup($values) {
    $data['name'] = $values['name'];
    $data['description'] = $values['description'];
    $rs = $this->getDb()->AutoExecute('core_module_groups', $data, 'UPDATE', 'id='.$this->getId());
    $id = $this->getId();
    $rs = $this->getDb()->Execute("DELETE FROM core_module_groups_modules WHERE gid=$id");
    $i = 1;
    foreach($values['checkbox'] as $key => $value) {
      $rs = $this->getDb()->Execute("INSERT INTO core_module_groups_modules SET gid='$id', mid='$key', rank=$i");
      $i++;
    }
  }

  function getGroups() {
    $this->getDb()->SetFetchMode(ADODB_FETCH_ASSOC);
    $groups = array();
    foreach ($this->getDb()->GetAll("SELECT * FROM core_module_groups ORDER BY rank") as $value) {
      $groups[$value['id']] = array('id' => $value['id'],
                                    'name' => $value['name'],
                                    'description' => $value['description'],
                                    'modules' => $this->getDb()->GetAssoc("SELECT t1.rank, t2.name FROM core_module_groups_modules AS t1 LEFT JOIN core_modules AS t2 ON t1.mid=t2.id WHERE t1.gid=$value[id] ORDER BY rank"));
    }
    return $groups;
  }

  function getGroup($id=null) {
    return $this->getDb()->GetRow("SELECT * FROM core_module_groups WHERE id=$id");
  }

  function moveUP($gid, $rank) {
    $rs = $this->getDb()->GetALL("SELECT * FROM core_module_groups_modules WHERE gid=$gid");
    foreach ($rs as $value) {
      if($value['rank'] == $rank-1) {
        $this->getDb()->Execute("UPDATE core_module_groups_modules SET rank=rank+1 WHERE gid='$gid' AND mid='$value[mid]'");
      }
      if($value['rank'] == $rank) {
        $this->getDb()->Execute("UPDATE core_module_groups_modules SET rank=rank-1 WHERE gid='$gid' AND mid='$value[mid]'");
      }
    }
    header('refresh: 0; url=/admin/modules/groups');
  }

  function moveDOWN($gid, $rank) {
    $rs = $this->getDb()->GetALL("SELECT * FROM core_module_groups_modules WHERE gid=$gid");
    foreach ($rs as $value) {
      if($value['rank'] == $rank+1) {
        $this->getDb()->Execute("UPDATE core_module_groups_modules SET rank=rank-1 WHERE gid='$gid' AND mid='$value[mid]'");
      }
      if($value['rank'] == $rank) {
        $this->getDb()->Execute("UPDATE core_module_groups_modules SET rank=rank+1 WHERE gid='$gid' AND mid='$value[mid]'");
      }
    }
    header('refresh: 0; url=/admin/modules/groups');
  }

  function gmoveUP($id) {
    $rank = $this->getDb()->GetOne("SELECT rank FROM core_module_groups WHERE id=$id");
    $rs = $this->getDb()->GetAll('SELECT * FROM core_module_groups');
    foreach ($rs as $value) {
      if($value['rank'] == $rank-1) {
        $this->getDb()->Execute("UPDATE core_module_groups SET rank=rank+1 WHERE id=$value[id]");
      }
      if($value['rank'] == $rank) {
        $this->getDb()->Execute("UPDATE core_module_groups SET rank=rank-1 WHERE id=$value[id]");
      }
    }
    header('refresh: 0; url=/admin/modules/groups');
  }

  function gmoveDOWN($id) {
    $rank = $this->getDb()->GetOne("SELECT rank FROM core_module_groups WHERE id=$id");
    $rs = $this->getDb()->GetAll('SELECT * FROM core_module_groups');
    foreach ($rs as $value) {
      if($value['rank'] == $rank+1) {
        $this->getDb()->Execute("UPDATE core_module_groups SET rank=rank-1 WHERE id=$value[id]");
      }
      if($value['rank'] == $rank) {
        $this->getDb()->Execute("UPDATE core_module_groups SET rank=rank+1 WHERE id=$value[id]");
      }
    }
    header('refresh: 0; url=/admin/modules/groups');
  }

  function deleteGroup() {
    $rs = $this->getDb()->Execute("DELETE FROM core_module_groups, core_module_groups_modules USING core_module_groups INNER JOIN core_module_groups_modules WHERE core_module_groups_modules.gid=core_module_groups.id AND core_module_groups.id='".$this->getId()."'");
    $rs = $this->getDb()->Execute("UPDATE core_module_groups SET rank=rank-1 WHERE id > '".$this->getId()."'");
    if(!$rs) { $this->setMsg($this->getDb()->ErrorMsg()); } else { return true; }
  }

} //end of class

?>